package com.mzy.linear_struct.ks;

import java.util.HashMap;

/**
 * @Author: mizy3
 * @Date: 2024/07/04/上午9:14
 * @Description:
 */
public class Pro03无重复字符的最长子串 {
    private static int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int ans = Integer.MIN_VALUE;
        int start = 0, end = 0;

        // 012345
        //   s
        // pwwkew
        //   e
        while (end < s.length()) {
            char c = s.charAt(end);

            // 遇到重复的了，负责更新start下标
            if (map.containsKey(c)) {
                start = Math.max(start, map.get(c) + 1);

            }
            map.put(c, end);
            ans = Math.max(end - start + 1, ans);
            end++;
        }


        return ans == Integer.MIN_VALUE ? 0 : ans;
    }
}
